package com.woniu.crowd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.crowd.infra.model.SelectLog;
import com.woniu.crowd.infra.model.ShopTest;
import com.woniu.crowd.infra.mapper.ShopTestMapper;
import com.woniu.crowd.service.SelectLogService;
import com.woniu.crowd.service.ShopTestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.crowd.service.form.AddShopTestpForm;
import com.woniu.exception.LXLGException;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.result.SpuResultInfo;
import com.woniu.result.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 内部甄选商品测评表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class ShopTestServiceImpl extends ServiceImpl<ShopTestMapper, ShopTest> implements ShopTestService {
@Resource
private ProductQueryClient productQueryClient;
@Resource
private SelectLogService selectLogService;
    @Override
    public void addShopTest(AddShopTestpForm addShopTestpForm) {
        Result<SpuResultInfo> select = productQueryClient.getSpuInfoBySpuId(addShopTestpForm.getSelectSpuId(),-1);
        if(select != null) {throw new LXLGException("商品信息不存在",500);
        }
        ShopTest shopTest = BeanUtil.copyProperties(addShopTestpForm, ShopTest.class);
        save(shopTest);
        SelectLog log = selectLogService.getOne(new QueryWrapper<SelectLog>().eq("select_product_id", addShopTestpForm.getSelectSpuId()));
        log.setTestId(shopTest.getId());
        selectLogService.updateById(log);
    }
}
